home *** CD-ROM | disk | FTP | other *** search
/ Aminet 51 / Aminet 51 (2002)(GTI - Schatztruhe)[!][Oct 2002].iso / Aminet / dev / c / minigl.lha / MiniGL / src_experimental / hardwarecull.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-03  |  3.4 KB  |  161 lines

  1. /*
  2. ** 02-04-02
  3. **
  4. ** MiniGL Interface to hardware backface culling by surgeon.
  5. ** Note that this interface is not yet tested with MiniGL.
  6. **
  7. ** Implementation:
  8. **
  9. ** The MGLSetHardwareCull function should be called from
  10. ** MGLSetstate function when the parameter is GL_CULL_FACE
  11. ** and with the assiciated flag propagandated.
  12. **
  13. ** The MGLUpdateHardwareCull function should be called
  14. ** from GLFrontFace and GLEnd with parameter 0, and in
  15. ** d_DrawTriangleStrip in all cases where a clipped or
  16. ** otherwise mangled strip id drawn, with the original
  17. ** vertexbuffer-position of the first vertex as parameter.
  18. ** With strip-sections, it is important that
  19. ** context->CurrentCullSign has not been flipped because it
  20. ** is used as reference.
  21. **
  22. **
  23. ** Optimizing performance:
  24. **
  25. ** W3D functions will only be called if needed. Therfore,
  26. ** it will be beneficial for performance to sort clipped
  27. ** strip-sections by original uneven vertexbuffer-positions
  28. ** of the first vertex before calling MGLUpdateHardwareCull,
  29. ** and in that case use the parameter 1.
  30. ** For optimal performance, those "uneven" primitives should
  31. ** be drawn last to ensure max 2 W3D_FrontFace calls pr
  32. ** rendered mesh (the original strip).
  33. **
  34. ** Alternatively, undefine CLEANACCESS. Note that it may not
  35. ** be compatible with future versions of Warp3D
  36. */
  37.  
  38. #include "sysinc.h"
  39.  
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43.  
  44.  
  45. #define CLEANACCESS 1
  46.  
  47. static GLint PrevCullSign = 0;
  48. static GLboolean HWCull_IsEnabled = GL_FALSE;
  49.  
  50. void MGLSetHardwareCull(GLcontext context, const GLboolean flag)
  51. {
  52.     if (context->w3dChipID == W3D_CHIP_VIRGE)
  53.         return; //does not support culling in hardware
  54.  
  55.     if(flag == GL_FALSE || context->CurrentCullSign == 0)
  56.     {
  57.         PrevCullSign = 0;
  58.  
  59.         if(HWCull_IsEnabled == GL_FALSE)
  60.         {
  61.             return;
  62.         }
  63.         else
  64.         {
  65.             W3D_SetState(context->w3dContext, W3D_CULLFACE, W3D_DISABLE);
  66.  
  67.             HWCull_IsEnabled = GL_FALSE;
  68.         }
  69.     }
  70.     else
  71.     {
  72.         if(HWCull_IsEnabled == GL_FALSE)
  73.         {
  74.             W3D_SetState(context->w3dContext, W3D_CULLFACE, W3D_ENABLE);
  75.  
  76.             HWCull_IsEnabled = GL_TRUE;
  77.         }
  78.  
  79.         if(PrevCullSign == context->CurrentCullSign)
  80.         {
  81.             return;
  82.         }
  83.         else if(context->CurrentCullSign == 1)
  84.         {
  85. #ifdef CLEANACCESS
  86.             W3D_SetFrontFace(context->w3dContext, W3D_CCW):
  87. #else
  88.             context->w3dContext->FrontFaceOrder = W3D_CCW;
  89. #endif
  90.         }
  91.         else if(context->CurrentCullSign == -1)
  92.         {
  93. #ifdef CLEANACCESS
  94.             W3D_SetFrontFace(context->w3dContext, W3D_CW):
  95. #else
  96.             context->w3dContext->FrontFaceOrder = W3D_CW;
  97. #endif
  98.         }
  99.  
  100.     PrevCullSign = context->CurrentCullSign;
  101.     }
  102. }
  103.  
  104.  
  105.  
  106. //NOTE: vstart is needed when a strip is sectioned
  107.  
  108. void MGLUpdateHardwareCull(GLcontext context, GLuint vstart)
  109. {
  110.     GLint cull_sign;
  111.  
  112.     if (context->w3dChipID == W3D_CHIP_VIRGE)
  113.         return; //does not support culling in hardware
  114.  
  115.     cull_sign = context->CurrentCullSign;
  116.  
  117.     if(cull_sign == 0 && HWCull_IsEnabled == GL_TRUE)
  118.     {
  119.         MGLSetHardwareCull(context, GL_FALSE);
  120.         return;
  121.     }
  122.  
  123.     if(PrevCullSign == 0)
  124.     {
  125.         MGLSetHardwareCull(context, GL_TRUE);
  126.     }
  127.  
  128.  
  129.     if(vstart > 0)
  130.     {
  131.         if(vstart%2)
  132.         cull_sign = -cull_sign; //invert
  133.     }
  134.  
  135.     if(cull_sign == PrevCullSign)
  136.     {
  137.         return;
  138.     }
  139.     else
  140.     {
  141.         PrevCullSign = cull_sign;
  142.     }
  143.  
  144.     if(cull_sign == 1)
  145.     {
  146. #ifdef CLEANACCESS
  147.         W3D_SetFrontFace(context->w3dContext, W3D_CCW):
  148. #else
  149.         context->w3dContext->FrontFaceOrder = W3D_CCW;
  150. #endif
  151.     }
  152.     else
  153.     {
  154. #ifdef CLEANACCESS
  155.         W3D_SetFrontFace(context->w3dContext, W3D_CW):
  156. #else
  157.         context->w3dContext->FrontFaceOrder = W3D_CW;
  158. #endif
  159.     }
  160. }
  161.